package controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Dept;
import pojo.Employee;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 徒有琴
 */
@Controller
public class ParamBindController {

    @RequestMapping("entity.html")
    public ResponseEntity<Dept> getEntity(RequestEntity<Dept> requestEntity){
        String header=requestEntity.getHeaders().getFirst("MyHeader");
        System.out.println(header);
        Dept dept=new Dept();
        dept.setName("测试部");
        HttpHeaders headers=new HttpHeaders();
        headers.set("MyResHeader","aaaaa");
        ResponseEntity<Dept> responseEntity=new ResponseEntity<Dept>(dept,headers, HttpStatus.OK);
        return responseEntity;
    }



    @RequestMapping("p1.html")
    public String test(@RequestParam(defaultValue = "0") int num) {
        System.out.println(num);
        return "/test.jsp";
    }

    @RequestMapping("add.html")
    public String add(Dept dept) {
        System.out.println(dept);
        return "test.jsp";
    }

    @RequestMapping("addEmp.html")
    public String add(Employee employee) {
        System.out.println(employee);
        return "test.jsp";
    }

    @RequestMapping("param6.html")
    public String param6(int[] answer) {
        System.out.println(Arrays.toString(answer));
        return "test.jsp";
    }

    @RequestMapping("param7.html")
    public String param7(@RequestBody List<String> list) {
        for (String s : list) {
            System.out.println(s);
        }
        return "test.jsp";
    }

    @RequestMapping("param8.html")
    public String param8(@RequestBody List<Dept> depts) {
        for (Dept dept : depts) {
            System.out.println(dept);
        }
        return "test.jsp";
    }

    @RequestMapping("param9.html")
    public String param8(@RequestBody Dept dept) {
        System.out.println(dept);
        if (dept.getEmployees() != null) {
            for (Employee employee : dept.getEmployees()) {
                System.out.println(employee);
            }

        }
        return "test.jsp";
    }

    @RequestMapping("param10.html")
    public String param10(@RequestBody List<Map<String, Object>> list) {
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            System.out.println(map);
            System.out.println("===================");
        }
        return "test.jsp";
    }

    @RequestMapping("api.html")
    public String api(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        session.setAttribute("sobj","我是session");
        return "test.jsp";
    }
}
